package ru.vsu.cs.kis.elections.client;

import java.net.InetAddress;
import java.net.UnknownHostException;

import ru.vsu.cs.kis.elections.GeneralConsts;
import ru.vsu.cs.kis.elections.dto.PrecinctDto;
import ru.vsu.cs.kis.elections.socketexchenge.CommandController;

public class ClientThread implements Runnable {

	private boolean isActive = false;
	
	private InetAddress serverAddress;
	
	private Integer port;
	
	private String address;
	
	private String password;
	
	private PrecinctDto precinct;

	public ClientThread() {
		try {
			serverAddress = InetAddress.getByName(LocalConsts.SERVER_IP);
		} catch (UnknownHostException e) {
			e.printStackTrace(System.err);
		}
		port = GeneralConsts.PORT;		
	}
	
	public ClientThread(String address, String password, String serverIP, Integer serverPort) {
		try {
			serverAddress = InetAddress.getByName(serverIP);
		} catch (UnknownHostException e) {
			e.printStackTrace(System.err);
		}
		this.port = serverPort;
		this.address=address;
		this.password=password;
	}


	@Override
	public void run() {
		System.out.println("client start");				
		isActive = true;
		CommandController controller=new CommandController(serverAddress,port);
		while (isActive) {	
			System.out.println(controller.ping());		
		}
	}
	
	/*private void addTask(Command command){
		System.out.println("try to add Thread");			
		try {
			socket = new Socket(address, port);
		} catch (IOException e) {
			e.printStackTrace(System.err);
		}			
		AsynTask newTask = new AsynTask(command);
		tasks.add(newTask);	
		PrecinctDto dto=new PrecinctDto(i, "some address ",1,"pw");
		dto.random();
		newTask.addCommand(new SendPrecinctCommand(socket,dto));				
		Thread Thr = new Thread(newTask);
		Thr.start();
		threadsCount++;		
	}*/

	public  void stop() {
		isActive = false;
		System.out.print("client stop \n");
	}
	
}
